package com.woniu.darryringmall.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.darryringmall.front.entity.Category;
import com.woniu.darryringmall.front.service.CategoryService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import com.woniu.darryringmall.utils.ResultStateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;

@RestController("adminCategory")
@RequestMapping("admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 接受页码数，和查找条件，返回筛选后的分页数据
     * @return
     */
    @RequestMapping("getPage")
    public ResponseData getCurrentPage(Integer current, Category category) {
        if (current == null) {
            current = 1;
        }
        Page<Category> page = new Page<>(current, 8);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if (category.getName() != null && category.getName().trim().length() != 0) {
            queryWrapper.like("name", category.getName());
        }
        if (category.getPId() != null) {
            queryWrapper.eq("pid", category.getPId());
        }
        if (category.getNavor() != null) {
            queryWrapper.eq("navor", category.getNavor());
        }
        page = categoryService.page(page, queryWrapper);
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", page);
    }

    @RequestMapping("list")
    public ResponseData getAll(String type) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (type != null) {
            wrapper.eq("type", type);
        }
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", categoryService.list(wrapper));
    }

    /**
     * 获取有层次的分级类别
     * @return
     */
    @RequestMapping("menus")
    public ResponseData getMenus() {
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", categoryService.getByPid());
    }

    /**
     * 接受页面数据，需要页面返回分类名称，分类是否导航，分类的父id,
     * @param category
     * @return
     */
    @RequestMapping("add")
    public ResponseData add(@RequestBody Category category) {
        if (category.getName() == null || category.getName().length() > 10 || category.getName().length() < 2) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", "名称长度必须在2-10位之间");
        }
        if (categoryService.getByName(category.getName()) != null) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL,"操作失败", "分类名称已经存在");
        }
        //状态默认是y
        category.setStatus("y");
        categoryService.save(category);
        return ResponseUtils.SUCCESS;
    }

    /**
     * 删除对应id的分类
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public ResponseData del(@PathVariable int id) {
        categoryService.delAll(id);
        return ResponseUtils.SUCCESS;
    }

    /**
     * 多条件更新
     * @param category
     * @return
     */
    @RequestMapping("update")
    public ResponseData update(@RequestBody Category category) {
        if (category.getName() == null || category.getName().length() > 10 || category.getName().length() < 2) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", "名称长度必须在2-10位之间");
        }
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", category.getName());
        queryWrapper.ne("id", category.getId());
        if (categoryService.getOne(queryWrapper) != null) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL,"操作失败", "分类名称已经存在");
        }
        UpdateWrapper<Category> updateWrapper = new UpdateWrapper<>();
        if (category.getName() != null) {
            updateWrapper.set("name", category.getName());
        }
        if (category.getPId() != null) {
            updateWrapper.set("pid", category.getPId());
        }
        if (category.getNavor() != null) {
            updateWrapper.set("navor", category.getNavor());
        }
        if (category.getType() != null) {
            updateWrapper.set("type", category.getType());
        }
        updateWrapper.eq("id", category.getId());
        categoryService.update(updateWrapper);
        return ResponseUtils.SUCCESS;
    }

    /**
     * 根据id获取对应的类别
     * @param id
     * @return
     */
    @RequestMapping("getById/{id}")
    public ResponseData getById(@PathVariable int id) {
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", categoryService.getById(id));
    }
}
